//+------------------------------------------------------------------------------------------------------------------+
#property description                                                                                        "[XU-MA]"
#define Version                                                                                              "[XU-MA]"
//+------------------------------------------------------------------------------------------------------------------+
#property link        "https://forex-station.com/viewtopic.php?p=1295409935#p1295409935"
#property description "THIS IS A FREE INDICATOR"
#property description "                                                      "
#property description "Welcome to XARD UNIVERSE"
#property description "Let light shine out of darkness and illuminate your world"
#property description "and with this freedom leave behind your cave of denial"
#property indicator_chart_window
#property indicator_buffers 20
string ID,xTimeFrame                     = "Current time frame",TimeFrame="Current time frame";
extern string Indicator                  = Version;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR01                      = "<<<==== [01] Candle Settings ====>>>";
  extern bool showCANDLES                = true;  int Type,getChartScale=WRONG_VALUE;  extern int cWick=2;
 extern color CandleUp                   = C'96,170,204',CandleWt=C'34,34,34',CandleDn=C'255,140,255';
       double candle0[],candle1[],candle2[],candle3[],candle4[],candle5[],candle6[],candle7[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR02                      = "<<<==== [02] T1MA Settings ====>>>"; double T1MAc[];
  extern bool showT1MA                   = false;  double T1MA[],T1MAa[],T1MAb[],T1MAv[];
   extern int T1MAper                    = 45,T1MAmode=1,T1MAwidth=10,T1MABGDwidth=12;
 extern color T1MAclrUP                  = clrBlue,T1MAclrDN=clrCrimson,T1MAclrBGD=C'100,130,160';
//+------------------------------------------------------------------------------------------------------------------+
extern string STR03                      = "<<<==== [03] S1MA Settings ====>>>"; double S1MAc[];
  extern bool showS1MA                   = false;  double S1MA[],S1MAa[],S1MAb[],S1MAv[];
   extern int S1MAper                    = 9,S1MAmode=1,S1MAwidth=5,S1MABGDwidth=7;
 extern color S1MAclrUP                  = clrBlue,S1MAclrDN=clrCrimson,S1MAclrBGD=C'100,130,160';
//+------------------------------------------------------------------------------------------------------------------+
extern string STR04                      = "<<<==== [04] BGDzones Settings ====>>>";
  extern bool showBGDzones               = false;
   extern int CCIPrice                   = PRICE_TYPICAL; double Dummy=-1; double colors[];
 extern color ColorUp                    = C'53,72,110',ColorDown=C'120,63,82',ColorWait=C'91,103,112';
//+------------------------------------------------------------------------------------------------------------------+
extern string STR05                      = "<<<==== [05] BOXtxt Settings ====>>>";
  extern bool showBOXtxt                 = false;
   extern int moveTxtLR                  = 0,moveTxtUD=0,FontSize=15; string BOXtxt; color BOXclr;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR06                      = "<<<==== [06] Alert Settings ====>>>";
  extern bool AlertsOn                   = false,
              AlertsOnCurrent            = true,
              AlertsMessage              = true,
              AlertsSound                = false,
              alertsNotify               = true,
              AlertsEmail                = false;  double trend[];
extern string soundfile                  = "alert1.wav"; int timeFrame,win; string indicatorFileName;
//template code start1
extern string             button_note1          = "------------------------------";
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_LOWER; // chart btn_corner for anchoring
extern string             btn_text              = "Candles";
extern string             btn_Font              = "Impact";
extern int                btn_FontSize          = 14;                             //btn__font size
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = C'120,120,120';
extern color              btn_background_color  = clrDarkRed;
extern color              btn_border_color      = clrDarkRed;
extern int                button_x              = 370;                                     //btn__x
extern int                button_y              = 26;                                     //btn__y
extern int                btn_Width             = 86;                                 //btn__width
extern int                btn_Height            = 26;                                //btn__height
extern string             button_note2          = "------------------------------";

bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix;
//template code end1

//+------------------------------------------------------------------------------------------------------------------+
string GenerateIndicatorName(const string target) //don't change anything here
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//+------------------------------------------------------------------------------------------------------------------+
string buttonId;

int OnInit()
{
   IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(IndicatorName);
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
      show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + "XUMACandles2020";
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);

// put init() here
   init2();
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------------------------------------------------------+
//don't change anything here
void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (ChartID(),buttonID);
      ObjectCreate    (ChartID(),buttonID,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}
//+------------------------------------------------------------------------------------------------------------------+
int deinit()
{
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);

//put deinit() here
   deinit2();
	return(0);
}
//+------------------------------------------------------------------------------------------------------------------+
//don't change anything here
bool recalc = true;

void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      start();
   }
}
//+------------------------------------------------------------------------------------------------------------------+
void OnChartEvent(const int id, //don't change anything here
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
}
//+------------------------------------------------------------------------------------------------------------------+
int start()
{
   handleButtonClicks();
   recalc = false;
   //put start () here
   start2();
      if (show_data)
         {
           ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
           if(showCANDLES)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
           SetIndexStyle(0,Type);
           SetIndexStyle(1,DRAW_NONE);
           SetIndexStyle(2,Type);
           SetIndexStyle(3,Type);
           SetIndexStyle(5,DRAW_NONE);
	        SetIndexStyle(4,Type);
           SetIndexStyle(6,Type);
           SetIndexStyle(7,Type); //ChartRedraw();
         }
      else
         {
           ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
           for (int banzai2=0; banzai2<8; banzai2++)
               SetIndexStyle(banzai2,DRAW_NONE);
         }
   return(0);
}
//+------------------------------------------------------------------------------------------------------------------+
//+----OnInit Function-----------------------------------------------------------------------------------------------+
   int init2(){int Buff=20,Buf=-1; IndicatorBuffers(Buff);   xTimeFrame=stringToTimeFrame(xTimeFrame);
   if(TimeFrame==0 || TimeFrame<Period()) TimeFrame=Period();  timeFrame=stringToTimeFrame(TimeFrame);
   ID = "b"; 
   //IndicatorDigits(Digits); IndicatorShortName(ID); 
   win=WindowFind(ID);
//+------------------------------------------------------------------------------------------------------------------+ 
   if(showCANDLES)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
   Buf+=1; SetIndexBuffer(Buf,candle0);    SetIndexStyle(Buf,Type,EMPTY,cWick,CandleUp);
   Buf+=1; SetIndexBuffer(Buf,candle1);    SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,candle2);    SetIndexStyle(Buf,Type,EMPTY,cWick,CandleWt);
   Buf+=1; SetIndexBuffer(Buf,candle3);    SetIndexStyle(Buf,Type,EMPTY,cWick,CandleDn);
   Buf+=1; SetIndexBuffer(Buf,candle4);    
   Buf+=1; SetIndexBuffer(Buf,candle5);    SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,candle6);    
   Buf+=1; SetIndexBuffer(Buf,candle7);    fSetBuffers();
//+------------------------------------------------------------------------------------------------------------------+
   if(showT1MA)           Type=DRAW_LINE; else Type=DRAW_NONE; int T1W=T1MAwidth,T1B=T1MABGDwidth;
   Buf+=1; SetIndexBuffer(Buf,T1MAc); SetIndexStyle(Buf,Type,0,T1B,T1MAclrBGD);
   Buf+=1; SetIndexBuffer(Buf,T1MA);  SetIndexStyle(Buf,Type,0,T1W,T1MAclrUP);  SetIndexDrawBegin(Buf,T1MAper+1);
   Buf+=1; SetIndexBuffer(Buf,T1MAa); SetIndexStyle(Buf,Type,0,T1W,T1MAclrDN);
   Buf+=1; SetIndexBuffer(Buf,T1MAb); SetIndexStyle(Buf,Type,0,T1W,T1MAclrDN);
   Buf+=1; SetIndexBuffer(Buf,T1MAv); SetIndexStyle(Buf,DRAW_NONE);
//+------------------------------------------------------------------------------------------------------------------+
   if(showS1MA)           Type=DRAW_LINE; else Type=DRAW_NONE; int S1W=S1MAwidth,S1B=S1MABGDwidth;
   Buf+=1; SetIndexBuffer(Buf,S1MAc); SetIndexStyle(Buf,Type,0,S1B,S1MAclrBGD);
   Buf+=1; SetIndexBuffer(Buf,S1MA);  SetIndexStyle(Buf,Type,0,S1W,S1MAclrUP);  SetIndexDrawBegin(Buf,S1MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S1MAa); SetIndexStyle(Buf,Type,0,S1W,S1MAclrDN);
   Buf+=1; SetIndexBuffer(Buf,S1MAb); SetIndexStyle(Buf,Type,0,S1W,S1MAclrDN);
   Buf+=1; SetIndexBuffer(Buf,S1MAv); SetIndexStyle(Buf,DRAW_NONE);
//+------------------------------------------------------------------------------------------------------------------+
   Buf+=1; SetIndexBuffer(Buf,colors);     SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,trend);      SetIndexStyle(Buf,DRAW_NONE);
//+------------------------------------------------------------------------------------------------------------------+
   if(Buff != Buf+1) Print("*******Buffer MisMatch!!!   ",Buff," ",Buf);
   for(int Bufx=0;Bufx<indicator_buffers;Bufx++){SetIndexLabel(Bufx,NULL);}  return(INIT_SUCCEEDED);}//End OnInit
//+----deinit Function-----------------------------------------------------------------------------------------------+
   int deinit2(){CleanUpOnIsle1(); return(0);}//End deinit
//+----OnCalculate Function------------------------------------------------------------------------------------------+
/*
   int OnCalculate(const int rates_total,
                   const int prev_calculated,
                   const datetime &time[],
                   const double &open[],
                   const double &high[],
                   const double &low[],
                   const double &close[],
                   const long &tick_volume[],
                   const long &volume[],
                   const int &spread[]){ 
                   */
int start2()
{
                   fSetBuffers();
//+------------------------------------------------------------------------------------------------------------------+
   int i,limit,counted_bars=IndicatorCounted(); if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--; limit = MathMin(Bars-counted_bars,Bars-1);
//+---TREND1 Function------------------------------------------------------------------------------------------------+
   if(T1MAv[limit]==-1) ClearPoint(limit,T1MAa,T1MAb);
   for(i=limit;i>=0;i--){T1MAv[i]=T1MAv[i+1];
   if(Close[i]>iMA(Symbol(),0,T1MAper,1,T1MAmode,PRICE_HIGH,i+1)) T1MAv[i]= 1;
   if(Close[i]<iMA(Symbol(),0,T1MAper,1,T1MAmode,PRICE_LOW,i+1))  T1MAv[i]=-1;
   if(T1MAv[i]==-1) T1MA[i]=iMA(Symbol(),0,T1MAper,0,T1MAmode,PRICE_MEDIAN,i+0); else 
                    T1MA[i]=iMA(Symbol(),0,T1MAper,0,T1MAmode,PRICE_MEDIAN,i+0); T1MAc[i]=T1MA[i];
   if(T1MAv[i]==-1) PlotPoint(i,T1MAa,T1MAb,T1MA);}//EO T1MA
//+---S1MA Function--------------------------------------------------------------------------------------------------+
   if(S1MAv[limit]==-1) ClearPoint(limit,S1MAa,S1MAb);
   for(i=limit;i>=0;i--){S1MAv[i]=S1MAv[i+1];
   if(Close[i]>iMA(Symbol(),0,S1MAper,0,S1MAmode,PRICE_HIGH,i+1)) S1MAv[i]= 1;
   if(Close[i]<iMA(Symbol(),0,S1MAper,0,S1MAmode,PRICE_LOW,i+1))  S1MAv[i]=-1;
   if(S1MAv[i]==-1) S1MA[i]=iMA(Symbol(),0,S1MAper,0,S1MAmode,PRICE_MEDIAN,i+0); else 
                    S1MA[i]=iMA(Symbol(),0,S1MAper,0,S1MAmode,PRICE_MEDIAN,i+0); S1MAc[i]=S1MA[i];
   if(S1MAv[i]==-1) PlotPoint(i,S1MAa,S1MAb,S1MA);}//EO S1MA
//+---BGD Zones Function---------------------------------------------------------------------------------------------+
   if(showBGDzones){ datetime tDummy=Dummy;  int CCIPeriod=T1MAper;
   if(timeFrame!=Period()){ 
   iCustom(NULL,timeFrame,indicatorFileName,"",CCIPeriod,CCIPrice,ID,ColorUp,ColorDown,Time[0],0,0); return(0);}
         else if(Dummy<=Time[0]) tDummy=Time[0];  static bool secondTime=false;
   if(secondTime){int count=0;
   for(;limit<Bars; limit++) if(colors[limit]!=colors[limit+1]){count++; if(count>1) break;}} else secondTime=true;
   double MaxValue=High[0]*10.0;   
   for(i=limit;i>=0;i--){if(i>Bars-2) continue;
        colors[i]=colors[i+1]; colors[i]=0;
   if(T1MAv[i]== 1 && S1MAv[i]== 1){colors[i]= 1;}
   if(T1MAv[i]==-1 && S1MAv[i]==-1){colors[i]=-1;}
   for(int index=i;index<Bars;index++) if(colors[index]!=colors[index+1]) break;
      string name=ID+"X"+Time[index+1]; ObjectDelete(name); ObjectDelete(ID+"X"+Time[1]);
    datetime lastTime = Time[i]; if (i==0) lastTime = tDummy;
      ObjectCreate(name,OBJ_RECTANGLE,0,Time[index+1],0,lastTime,MaxValue);
         ObjectSet(name,OBJPROP_BACK,true);
            switch ((int)colors[i]){
      case -1 : ObjectSet(name,OBJPROP_COLOR,ColorDown); break;
      case  0 : ObjectSet(name,OBJPROP_COLOR,ColorWait); break;
      case  1 : ObjectSet(name,OBJPROP_COLOR,ColorUp);   break;}}}//End of BGD Zones
//+---DIRECTIONAL TRADE----------------------------------------------------------------------------------------------+
   for(i=limit;i>=0;i--){trend[i]=trend[i+1]; trend[i]=0; candle(2,i); BOXtxt=" WAITING . . ."; BOXclr=C'40,40,40';
   if(T1MAv[i]== 1 && S1MAv[i]== 1){candle(1,i); trend[i]= 1; BOXtxt="   UP TREND"; BOXclr=clrGreen;}
   if(T1MAv[i]==-1 && S1MAv[i]==-1){candle(3,i); trend[i]=-1; BOXtxt="DOWN TREND"; BOXclr=clrCrimson;}}
//+---BOXtxt DISPLAY-------------------------------------------------------------------------------------------------+
	if(showBOXtxt){Panels(ID+"X070",0,-1,"ggggggg",FontSize+3,"Webdings",BOXclr);
                  Labels(ID+"X071",6,-2); ObjectSetText(ID+"X071",BOXtxt,FontSize,"Arial Black",clrSilver);}
//+------------------------------------------------------------------------------------------------------------------+
   manageAlerts(); return(0);}//End OnCalculate
//+------------------------------------------------------------------------------------------------------------------+
   void ClearPoint(int i,double& first[],double& second[]){
   if((second[i]!=EMPTY_VALUE) && (second[i+1]!=EMPTY_VALUE)) second[i+1]= EMPTY_VALUE; else
   if((first[i]!=EMPTY_VALUE) && (first[i+1]!=EMPTY_VALUE) && (first[i+2]==EMPTY_VALUE)) first[i+1]=EMPTY_VALUE;}
//+------------------------------------------------------------------------------------------------------------------+
   void PlotPoint(int i,double& first[],double& second[],double& from[]){if(first[i+1] == EMPTY_VALUE){
   if(first[i+2] == EMPTY_VALUE) {first[i]=from[i]; first[i+1]=from[i+1]; second[i]=EMPTY_VALUE;}  else {
   second[i]=from[i]; second[i+1]=from[i+1]; first[i]=EMPTY_VALUE;}} else {first[i]=from[i]; second[i]=EMPTY_VALUE;}}
//+----Candle Function-----------------------------------------------------------------------------------------------+
   void candle(int direction,int i){
   double bodyHigh=MathMax(Open[i],Close[i]),bodyLow=MathMin(Open[i],Close[i]),high=High[i],low=Low[i];
   candle0[i]=low; candle1[i]=low; candle2[i]=low; candle3[i]=low;
   candle4[i]=bodyLow; candle5[i]=bodyLow; candle6[i]=bodyLow; candle7[i]=bodyLow;
   switch (direction) {case 1:candle0[i]=high; candle4[i]=bodyHigh; return;
                       case 2:candle2[i]=high; candle6[i]=bodyHigh; return;
                       case 3:candle3[i]=high; candle7[i]=bodyHigh; return;}}
//+------------------------------------------------------------------------------------------------------------------+
   void fSetBuffers(){int iChartScale = int(ChartGetInteger(0,CHART_SCALE)); if(getChartScale == iChartScale) return;
	getChartScale = iChartScale; int iW=0,rW=0; switch(iChartScale){
		case 0: iW=1; rW=1; break; case 1: iW=1; rW=2; break; case 2: iW= 2; rW= 3; break;
		case 3: iW=4; rW=5; break; case 4: iW=6; rW=9; break; case 5: iW=14; rW=17; break;}
	if(showCANDLES)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
	SetIndexStyle(4,Type,0,iW,CandleUp);
   SetIndexStyle(6,Type,0,iW,CandleWt);
   SetIndexStyle(7,Type,0,iW,CandleDn); ChartRedraw();}
//+------------------------------------------------------------------------------------------------------------------+
   void Panels(string aa3,int x,int y,string Text,int fontSize,string Font,color Color){
         ObjectCreate(aa3,OBJ_LABEL,0,0,0);
            ObjectSet(aa3,OBJPROP_CORNER,2);
            ObjectSet(aa3,OBJPROP_XDISTANCE,x+moveTxtLR);
            ObjectSet(aa3,OBJPROP_YDISTANCE,y+moveTxtUD);
            ObjectSet(aa3,OBJPROP_BACK,false);
   ObjectSetInteger(0,aa3,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,aa3,OBJPROP_SELECTED,false);
    ObjectSetString(0,aa3,OBJPROP_TOOLTIP,"\n");
        ObjectSetText(aa3,Text,fontSize,Font,Color);}
//+------------------------------------------------------------------------------------------------------------------+
   void Labels(string aa4,int x,int y){ ObjectCreate(aa4,OBJ_LABEL,0,0,0);
            ObjectSet(aa4,OBJPROP_CORNER,2);
            ObjectSet(aa4,OBJPROP_XDISTANCE,x+moveTxtLR);
            ObjectSet(aa4,OBJPROP_YDISTANCE,y+moveTxtUD);
            ObjectSet(aa4,OBJPROP_BACK,false);
   ObjectSetInteger(0,aa4,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,aa4,OBJPROP_SELECTED,false);
    ObjectSetString(0,aa4,OBJPROP_TOOLTIP,"\n");}
//+---Timeframe Function---------------------------------------------------------------------------------------------+
   string sTfTable[]={"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
   int    iTfTable[]={1,5,15,30,60,240,1440,10080,43200};
//+------------------------------------------------------------------------------------------------------------------+
   int stringToTimeFrame(string tfs){ tfs=stringUpperCase(tfs);  for(int i=ArraySize(iTfTable)-1; i>=0; i--)
   if(tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],_Period));  return(_Period);}
//+------------------------------------------------------------------------------------------------------------------+
   string timeFrameToString(int tf){
   for(int i=ArraySize(iTfTable)-1;i>=0;i--)  if(tf==iTfTable[i]) return(sTfTable[i]); return("");}
//+------------------------------------------------------------------------------------------------------------------+
   string stringUpperCase(string str){ string s=str; for(int length=StringLen(str)-1; length>=0; length--){
   int tchar=StringGetChar(s,length); if((tchar>96 && tchar<123) || (tchar>223 && tchar<256))
   s=StringSetChar(s,length,tchar-32); else if(tchar>-33 && tchar<0) s=StringSetChar(s,length,tchar+224);} return(s);}
//+----Clean Chart Function------------------------------------------------------------------------------------------+
   void CleanUpOnIsle1(){string namem; for(int m=ObjectsTotal()-1; m>=0; m--){namem=ObjectName(m);
   if(StringSubstr(namem,0,StringLen(ID))==ID) {ObjectDelete(namem);}}}//EOF
//+----END OF FILE---------------------------------------------------------------------------------------------------+
   void manageAlerts(){if(AlertsOn){if(AlertsOnCurrent)
   int whichBar=0; else whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,0,whichBar));
   if(trend[whichBar] != trend[whichBar+1]){
   if(trend[whichBar] == 1) doAlert(whichBar,"UP");
   if(trend[whichBar] ==-1) doAlert(whichBar,"DOWN");}}}
//+------------------------------------------------------------------------------------------------------------------+
   void doAlert(int forBar, string doWhat){
   static string previousAlert="nothing"; static datetime previousTime; string msg;
   if(previousAlert != doWhat || previousTime != Time[forBar]){previousAlert=doWhat; previousTime=Time[forBar];
   msg=StringConcatenate(Symbol()," : Direction changed to ",doWhat);
   if(AlertsMessage) Alert(msg); if(AlertsSound) PlaySound("alert.wav"); if(alertsNotify) SendNotification(msg);
   if(AlertsEmail) SendMail(msg,msg+"\nLocal time "+TimeToStr(TimeLocal(),
   TIME_SECONDS)+""+"\nBroker time "+TimeToStr(TimeCurrent(),TIME_SECONDS));}}//End ManageAlerts
//+------------------------------------------------------------------------------------------------------------------+